package cn.git.bbs.core.service.product;

import cn.git.bbs.core.bean.product.*;
import cn.git.bbs.core.dao.product.ColorMapper;
import cn.git.bbs.core.dao.product.ProductMapper;
import cn.git.bbs.core.dao.product.SkuMapper;
import cn.itcast.common.page.Pagination;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {


    @Autowired
    ProductMapper productMapper;
    public Pagination selectPaginationByQuery(String name, Integer isShow, Integer pageNo) {

        ProductQuery productQuery = new ProductQuery();

        productQuery.setPageNo(Pagination.cpn(pageNo));

        productQuery.setPageSize(10);


        StringBuffer str = new StringBuffer();

        if (name != null){
            ProductQuery.Criteria criteria = productQuery.createCriteria();
            criteria.andNameEqualTo(name);
            str.append("name=").append(name);
        }
        if (isShow != null){
            ProductQuery.Criteria criteria = productQuery.createCriteria();
            criteria.andIsShowEqualTo(isShow);
            str.append("&isShow=").append(isShow);
        }

        Pagination pagination = new Pagination(
                productQuery.getPageNo(),
                productQuery.getPageSize(),
                productMapper.countByExample(productQuery)
        );

        pagination.setList(productMapper.selectByExample(productQuery));

//        分页展示
        String url = "/product/list.do";

        pagination.pageView(url,str.toString());

        return pagination;
    }

    public Product selectProductById(Long id) {

        return productMapper.selectByPrimaryKey(id);

    }

    public void updateProductById(Product product) {

        productMapper.updateByPrimaryKey(product);
    }
    @Autowired
    ColorMapper colorMapper;
    public List<Color> selectColorList() {

        ColorQuery colorQuery = new ColorQuery();
        colorQuery.createCriteria().andParentIdNotEqualTo(0L);
        List<Color> colors = colorMapper.selectByExample(colorQuery);
        return colors;
    }
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    Jedis jedis;
    public void insertProduct(Product product) {
        //返回ID
        Long pno = jedis.incr("pno");
//		下架状态 后台程序写的
        //保存SKU
        product.setId(pno);
        product.setIsShow(0);
//		删除  后台程序写的不删除
        product.setIsDel(1);
        productMapper.insertSelective(product);
        String[] colors = product.getColors().split(",");
        String[] sizes = product.getSizes().split(",");
        //颜色
        for (String color : colors) {
            for (String size : sizes) {
                //保存SKU
                Sku sku = new Sku();
                //商品ＩＤ
                sku.setProductId(product.getId());
                //颜色
                sku.setColorId(Long.parseLong(color));
                //尺码
                sku.setSize(size);
                //市场价
                sku.setMarketPrice(999f);
                //售价
                sku.setPrice(666f);
                //运费
                sku.setDeliveFee(8f);
                //库存
                sku.setStock(0);
                //限制
                sku.setUpperLimit(200);
                //时间
                sku.setCreateTime(new Date());
                skuMapper.insertSelective(sku);

            }
        }
    }

    @Autowired
    private JmsTemplate jmsTemplate;
    public void isShow(Long[] ids) {
        Product product = new Product();
        product.setIsShow(1);
        for (final Long id: ids) {
            //保存商品状态
            product.setId(id.longValue());
            productMapper.updateByPrimaryKeySelective(product);
            //保存到solr服务器
            jmsTemplate.send(new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage(String.valueOf(id));
                }
            });

            /*SolrInputDocument document = new SolrInputDocument();
            Product p = productMapper.selectByPrimaryKey(id.longValue());
            document.setField("id",id);
            document.setField("name_ik",p.getName());
            document.setField("url",p.getImages()[0]);
            document.setField("brandId",p.getBrandId());
            //查询价格   select price form bbs_sku where product_id = 442 order by price asc limit 0,1
            SkuQuery skuQuery = new SkuQuery();
            skuQuery.setPageNo(1);
            skuQuery.setPageSize(1);
            skuQuery.createCriteria().andProductIdEqualTo(id.longValue());
            skuQuery.setOrderByClause("price");
            List<Sku> skus = skuMapper.selectByExample(skuQuery);
            document.setField("price",skus.get(0).getPrice());
            try {
                solrServer.add(document);
                solrServer.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }*/
        }
    }

}
